Skip to content

fix: range-check Calibur key settings so expiration cannot set the isAdmin bit - #222

Merged
Sednaoui merged 1 commit into
devfrom
fix/calibur-key-settings
Jul 23, 2026
Merged

fix: range-check Calibur key settings so expiration cannot set the isAdmin bit#222
Sednaoui merged 1 commit into
devfrom
fix/calibur-key-settings

Conversation

@sherifahmed990

@sherifahmed990 sherifahmed990 commented Jul 22, 2026

Copy link
Copy Markdown
Member

packKeySettings shifted expiration into bits 160+ without validating it fits the contract's uint40 field. A millisecond timestamp (≥ 2^40) overflowed exactly into bit 200 — the
isAdmin flag — registering the key as admin and bypassing the explicit guardrail in createRegisterKeyMetaTransactions.

Both expiration and hook are now range-checked, with an error hint about seconds vs milliseconds since a ms timestamp is the most likely way to hit this.

Tests added in test/calibur/packKeySettings.test.js; full offline suite passes.

Summary by CodeRabbit

  • Bug Fixes

    • Added validation for key expiration values and hook addresses.
    • Prevented invalid values from corrupting packed account settings or administrative flags.
    • Added clearer errors when expiration values use milliseconds instead of seconds.
  • Tests

    • Added coverage for valid packing and out-of-range input handling.

…Admin bit

packKeySettings shifted expiration into bits 160+ without validating it
fits the contract's uint40 field. A millisecond timestamp (>= 2^40)
overflowed exactly into bit 200 — the isAdmin flag — registering the key
as admin and bypassing the explicit guardrail in
createRegisterKeyMetaTransactions. Both expiration and hook are now
range-checked with a hint about seconds vs milliseconds.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

packKeySettings now validates expiration and hook against their on-chain widths before packing. New tests cover valid expiration packing, overflow prevention, and invalid hook rejection.

Changes

Calibur key settings validation

Layer / File(s) Summary
Packing bounds and validation tests
src/account/Calibur/Calibur7702Account.ts, test/calibur/packKeySettings.test.js
packKeySettings rejects out-of-range uint40 expiration and uint160 hook values, while tests verify valid packing and RangeError handling.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: sednaoui, andrewwahid

Poem

I’m a rabbit guarding bits with care,
No overflow hops into flags up there.
Seconds pack neatly, hooks stay right,
Bad values meet a RangeError bite.
✨ Fields stay safe through day and night.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding range checks to Calibur key settings to prevent expiration overflow into isAdmin.
Description check ✅ Passed The description covers the summary and testing, but it lacks the template's explicit Risk / Compatibility section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/calibur/packKeySettings.test.js`:
- Around line 18-21: Update the test case “rejects a milliseconds timestamp
instead of leaking into isAdmin” to assert that the thrown RangeError message
includes the actionable guidance indicating the expiration value must use
seconds rather than milliseconds. Keep the existing RangeError assertion and
verify the message content on the same thrown error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 006b19ef-8ee1-4130-84b8-0ba01dc77960

📥 Commits

Reviewing files that changed from the base of the PR and between a4f98c7 and cea226c.

📒 Files selected for processing (2)
  • src/account/Calibur/Calibur7702Account.ts
  • test/calibur/packKeySettings.test.js

Comment on lines +18 to +21
test('rejects a milliseconds timestamp instead of leaking into isAdmin', () => {
const ms = 1786000000000n; // >= 2^40: would set bit 200 (isAdmin)
expect(() => Calibur.packKeySettings({ expiration: ms })).toThrow(RangeError);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the actionable expiration error hint.

This test only checks RangeError; it would still pass if the promised “milliseconds instead of seconds” guidance were removed.

Suggested assertion
-        expect(() => Calibur.packKeySettings({ expiration: ms })).toThrow(RangeError);
+        expect(() => Calibur.packKeySettings({ expiration: ms })).toThrow(
+            /milliseconds instead of seconds/i,
+        );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
test('rejects a milliseconds timestamp instead of leaking into isAdmin', () => {
const ms = 1786000000000n; // >= 2^40: would set bit 200 (isAdmin)
expect(() => Calibur.packKeySettings({ expiration: ms })).toThrow(RangeError);
});
test('rejects a milliseconds timestamp instead of leaking into isAdmin', () => {
const ms = 1786000000000n; // >= 2^40: would set bit 200 (isAdmin)
expect(() => Calibur.packKeySettings({ expiration: ms })).toThrow(
/milliseconds instead of seconds/i,
);
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/calibur/packKeySettings.test.js` around lines 18 - 21, Update the test
case “rejects a milliseconds timestamp instead of leaking into isAdmin” to
assert that the thrown RangeError message includes the actionable guidance
indicating the expiration value must use seconds rather than milliseconds. Keep
the existing RangeError assertion and verify the message content on the same
thrown error.

@Sednaoui
Sednaoui merged commit e76c8e6 into dev Jul 23, 2026
3 checks passed
@Sednaoui
Sednaoui deleted the fix/calibur-key-settings branch July 23, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants